//
//  MyClass.m
//  iOSUtils
//
//  Created by HJC on 11-9-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "_iOSUtils_NSStringAddtions.h"
#import <CommonCrypto/CommonDigest.h>


@implementation  NSString(_iOSUtils_Addtions)


- (BOOL) caseInsensitiveEqualTo:(NSString*)string
{
	return ([self caseInsensitiveCompare:string] == NSOrderedSame);
}


- (NSString*) stringByTrimmingWhitespaces
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}


- (NSString*) stringByEncodingToMd5
{
    const char *cStr = [self UTF8String];
	unsigned char result[16];
	CC_MD5( cStr, strlen(cStr), result );
	return [NSString stringWithFormat:
			@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]
			];	
}


+ (NSString*) stringWithFloat:(CGFloat)value
{
    // 转成字符串，有可能有多余的0, 例如 3.00000, 下面的代码就将多余的0去掉
    NSString* string = [NSString stringWithFormat:@"%lf", value];
    
    // 没有小数点，返回
    NSRange range = [string rangeOfString:@"." options:NSBackwardsSearch];
    if (range.location == NSNotFound)
    {
        return string;
    }
    
    // 从后往前扫描, 查找小数点或第一个非0数字的位置，之后去掉多余的字串
    for (NSInteger idx = [string length] - 1; idx >= 0; idx--)
    {
        unichar c = [string characterAtIndex:idx];
        if (c == L'.')
        {
            return [string substringToIndex:idx];
        }
        else if (c != L'0')
        {
            return [string substringToIndex:idx + 1];
        }
    }
    return string;
}

@end
